//
//  BACircelDraw.swift
//  BadAir
//
//  Created by D on 2018/4/2.
//  Copyright © 2018年 D. All rights reserved.
//

import UIKit

class BACircelDraw: UIView {
    
    
    var value: String? {
        didSet{
            setNeedsDisplay()
        }
    }
    
    override func draw(_ rect: CGRect) {
        
        let colors = BAAqiViewModel.aqiValueWithColor(value: (value ?? "0"))
        
        let gradient = CGGradient(colorsSpace: nil, colors: [colors[0] , colors[1]] as CFArray, locations: [0 , 1])!
        
        let frameGree = CGRect(x: 0, y: 0, width: 12, height: 12)
        let frameLess = CGRect(x: 2, y: 2, width: 8, height: 8)
        // 开始绘制
        let context = UIGraphicsGetCurrentContext()!
        
        
        //填充当前矩形区域
        context.addEllipse(in: frameGree)
        context.clip()
        context.saveGState()
        
        context.drawLinearGradient(gradient, start: CGPoint(x: 2, y: 2), end: CGPoint(x: 10, y: 10), options: [.drawsBeforeStartLocation , .drawsAfterEndLocation])
        context.restoreGState()
        
        context.addEllipse(in: frameLess)
        UIColor.white.set()
        context.fillPath()
        
        
//                context.addEllipse(in: frameLess)
//                UIColor.white.set()
//                context.fillPath()
        
        
//        context.fillPath()
        //        context.strokePath()
        //        context.saveGState()
        //        let path = CGPath.init(ellipseIn: frameLess, transform: nil)
        //        context.addPath(path)
        
//        context.strokePath()
//        context.saveGState()
    }
}
